using System.Linq;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Params;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Mvc;

namespace ArticleManagementSystem.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class LikeController : ControllerBase
    {
        private readonly IRepostisory<Like> _likeRepostisory;

        public LikeController(IRepostisory<Like> likeRepostisory)
        {
            _likeRepostisory = likeRepostisory;
        }

        /// <summary>
        /// 获取点赞表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetLike()
        {
            var like = _likeRepostisory.Table.GroupBy(p => new { p.ArticleId })
                    .Select(p => new { ArticleId = p.Key.ArticleId, count = p.Sum(s => 1) }).ToList();

            return new
            {
                code = 200,
                data = like,
                msg = "请求成功"
            };
        }
        /// <summary>
        /// 通过id获取点赞表
        /// </summary>
        /// <returns></returns>
        [HttpGet("{id}")]
        public dynamic ToIdGetLike(int id)
        {
            var like = _likeRepostisory.Table.Where(x => x.ArticleId == id).GroupBy(p => new { p.ArticleId })
                    .Select(p => new { ArticleId = p.Key.ArticleId, count = p.Sum(s => 1) }).ToList();

            return new
            {
                code = 200,
                data = like,
                msg = "请求成功"
            };
        }


        /// <summary>
        /// 点赞和去消点赞
        /// </summary>
        /// <param name="id"></param>
        /// <param name="getLike"></param>
        /// <returns></returns>
        [HttpPost]
        public dynamic PostLike(GetLike getLike)
        {
            var userId = getLike.UserId;
            var articleId = getLike.ArticleId;
            var like = _likeRepostisory.Table;
            var isUser = _likeRepostisory.Table.Where(x => x.UserId == userId && x.ArticleId == articleId).FirstOrDefault();

            if (isUser == null)
            {
                var addLike = new Like
                {
                    UserId = userId,
                    ArticleId = articleId,
                };
                _likeRepostisory.Insert(addLike);
                return new
                {
                    code = 200,
                    data = true,
                    msg = "点赞成功"
                };


            }
            else
            {
                _likeRepostisory.Deleted(isUser.Id);
                return new
                {
                    code = 404,
                    data = false,
                    msg = "取消点赞"
                };
            }

        }


        /// <summary>
        /// 判断该用户是否点赞
        /// </summary>
        /// <param name="id"></param>
        /// <param name="getLike"></param>
        /// <returns></returns>
        [HttpPost,Route("userlike")]
        public dynamic userLike(GetLike getLike)
        {
            var userId = getLike.UserId;
            var articleId = getLike.ArticleId;
            var like = _likeRepostisory.Table;
            var isUser = _likeRepostisory.Table.Where(x => x.UserId == userId && x.ArticleId == articleId).FirstOrDefault();
            
            return new {
                code = 200,
                data = isUser,
                msg = "获取成功"
            };
        }
    }
}